<?php

function checkquyentruycap_bang($mucbang) {
    $sscapquyen = $_SESSION['capquyen'];
    if ($sscapquyen != $mucbang)
        echo "<script>alert('Không có quyền truy cập vào phần quản lý này.'); window.location='index.php';</script>";
}

function EncodeSpecialChar($content) {  //insert table
    $content = trim($content);
    $content = addslashes($content);
    $content = htmlspecialchars($content);
    return $content;
}

function ConvertDate_time_db($predate) { // insert table
    $arr1 = explode(" ", $predate);
    $arr = substr($arr1[0], 0, 10);
    $date = explode("-", $arr);
    return $date[2] . "-" . $date[1] . "-" . $date[0] . " " . $arr1[1];
}

function ConvertDate_time_load($predate) { // load data
    $arr1 = explode(" ", $predate);
    $arr = substr($arr1[0], 0, 10);
    $date = explode("-", $arr);
    return $date[2] . "/" . $date[1] . "/" . $date[0] . " " . $arr1[1];
}

//Function getid
function getid() {
    include "connect.php";
    $sql = "select max(id_baiviet) AS maxid from baiviet";
    $kq = mysqli_query($conn, $sql);
    while ($r = mysqli_fetch_array($kq)) {
        $maxid = $r['maxid'];
        $k = $maxid + 1;
    }
    return $k;
}

function addCart($id, $sl) {
    global $cart;
    if (isset($cart[$id])) {
        $cart[$id] = $cart[$id] + $sl;
    } else {
        $cart[$id] = $sl; 
    }
    $_SESSION["gh"] = $cart;
}

function deleteCart($item) {
    global $cart;

    if (isset($cart[$item])) {
        unset($cart[$item]);
        $_SESSION["gh"] = $cart;
    }
}

function updateCart($id, $sl) {
    global $cart;

    if (isset($cart[$id])) {
        $cart[$id] = $sl;
        if ($sl < 1) {
            unset($cart[$id]);
            echo "Xóa...";
        }
        $_SESSION["gh"] = $cart;
    }
}